﻿using System.Security.Cryptography;

namespace CacheNaga.HashAlgorithms
{
	/// <summary>
	/// 在MD5算法的基础上修改的哈希算法
	/// 该算法的散列度比FNV32修改的要高，尤其是对于第一个节点。其次，该算法对于当添加或减少机器时的数据迁移更少
	/// 范维肖 2009/05
	/// </summary>
	internal static class EnhancedMD5HashAlgorithm
	{
		public static uint Md5(string s)
		{
			byte[] bytes = System.Text.Encoding.Default.GetBytes(s);
			using (MD5CryptoServiceProvider provider = new MD5CryptoServiceProvider())
			{
				var keys = provider.ComputeHash(bytes);

				uint hash = (
					(uint)(keys[3] & 0xFF) << 24 |
					(uint)(keys[2] & 0xFF) << 16 |
					(uint)(keys[1] & 0xFF) << 8 |
					(uint)(keys[0] & 0xFF));

				return hash;
			}
		}
	}
}
